package com.dcsgo.socketPlay.server;

import com.dcsgo.socketPlay.common.Ball;
import com.dcsgo.socketPlay.common.Message;
import com.dcsgo.socketPlay.common.MessageType;
import com.dcsgo.socketPlay.server.game.BallManager;

import java.io.IOException;
import java.net.Socket;

/**
 * @author DCSGO
 * @version 1.0
 * 为建立了连接的Client提供服务的线程类
 */
public class ServeClientsThread extends Thread {
    private boolean loop = true;
    private final Socket clientSocket;
    private final Integer clientId;

    private void sendConnectedSuccessfully() throws IOException {
        System.out.println(Thread.currentThread().getName() + " 有客户端请求建立连接!");
        //封装要发送的消息 sendMes
        Message sendMes = new Message();
        sendMes.setMesType(MessageType.CONNECTED_SUCCESSFULLY);
        sendMes.setReceiveClientId(clientId);
        sendMes.setProperties(Server.getGameProperties());
        //为当前玩家创建一个ball并添加至BallManager
        int[] position = BallManager.getRandomPosition();
        Ball replaceBall = new Ball(position[0], position[1], 5);
        BallManager.put(clientId, replaceBall);
        sendMes.setReplaceBall(replaceBall);
        sendMes.setBallList(BallManager.getBallList());

        //发送消息 sendMes
        Services.sendMessage(sendMes, clientSocket);
        System.out.println(Thread.currentThread().getName() + " 已发送连接建立成功消息");
    }

    private void sendRenewBalls() throws IOException {
        Message message = new Message();
        message.setMesType(MessageType.RENEW_BALLS);
        message.setBallList(BallManager.getBallList());
        message.setReplaceBall(BallManager.get(clientId));
        Services.sendMessage(message, clientSocket);
    }

    @Override
    public void run() {
        while (loop) {
            try {
                Message receiveMes = Services.receiveMessage(clientSocket);
                if (receiveMes == null) {
                    continue;
                }
                switch (receiveMes.getMesType()) {
                    case MessageType.ESTABLISH_CONNECTION://请求建立连接
                        sendConnectedSuccessfully();
                        break;
                    case MessageType.PLAYER_OPERATION://玩家移动了小球
                        //更新玩家操作的ball
                        BallManager.put(clientId, receiveMes.getReplaceBall());
                        //向所有玩家广播ballList
                        sendRenewBalls();
                        break;
                    case MessageType.CLIENT_OFFLINE://客户端下线
                        Services.sendMessage(receiveMes, clientSocket);
                        ClientSocketManager.Disconnect(clientId);
                        System.out.println(Thread.currentThread().getName() + " " + clientId + " 客户端下线!");
                        break;
                    default:
                        System.out.println(Thread.currentThread().getName() + " 没有该类型的消息!");
                        break;
                }
            } catch (IOException | ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
    }


    public boolean isLoop() {
        return loop;
    }

    public Socket getClientSocket() {
        return clientSocket;
    }

    public Integer getClientId() {
        return clientId;
    }

    public ServeClientsThread(Integer clientId, Socket clientSocket) {
        this.clientId = clientId;
        this.clientSocket = clientSocket;
    }

    public void shutdown() {
        this.loop = false;
    }
}
